<template>
  <teleport to="#loader">
    <div class="d-flex justify-content-center align-items-center h-100 w-100 loading-container"
         :style="{ backgroundColor: background || '' }">
      <div class="loading-content">
        <div class="spinner-border text-primary"
             role="status">
          <span class="visually-hidden">{{ text || 'loading' }}</span>
        </div>
        <p v-if="text"
           class="text-primary">{{ text }}</p>
      </div>
    </div>
  </teleport>
</template>

<script lang='ts'>
import { defineComponent } from 'vue'
import useDomCreate from '../hook/useDomCreate'
export default defineComponent({
  name: 'Loader',
  props: {
    text: {
      type: String
    },
    background: {
      type: String
    }
  },
  setup() {
    // 使用Teleport将组件模板移动到与DOM中Vue app同级 结构清晰明了便于维护
    useDomCreate('loader')
    return {}
  }
})
</script>

<style>
.loading-container {
  background: rgba(255, 255, 255, .5);
  z-index: 100;
  position: fixed;
  top: 0;
  left: 0;
  text-align: center;
}
</style>